//
//  LTFReadView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/13.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFReadView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFReadModel.h"
#import "LTFReadFirstCell.h"
#import "LTFReadSecondCell.h"
#import "LTFReadThirdCell.h"
#import "LTFReadFourthCell.h"
#import "LTFReadFifthCell.h"
#import "LTFNetWorkingTool.h"
#import "ReadContentViewController.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@interface LTFReadView ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@end

@implementation LTFReadView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self createData];
    }
    return self;
}
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tag = 1003;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LTFReadModel *model = self.tableViewArray[indexPath.row];
    if (model.imgnewextra == nil) {
        
        if (model.imgsrc == nil) {
            static NSString *fifthReuse = @"fifthReuse";
            LTFReadFifthCell *cell = [tableView dequeueReusableCellWithIdentifier:fifthReuse];
            if (!cell) {
                cell = [[[LTFReadFifthCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:fifthReuse] autorelease];
            }
            cell.titleLabel.text = model.title;
            cell.sort.text = model.source;
            return cell;
        }
        else {
            
            
            if ([model.recType intValue] == -1) {
                static NSString *firstReuse = @"firstReuse";
                LTFReadFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:firstReuse];
                if (!cell) {
                    cell = [[[LTFReadFirstCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:firstReuse] autorelease];
                }
                [cell.picImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
                cell.titleLabel.text = model.title;
                cell.sort.text = model.source;
                return cell;
            }
            else if ([model.recType intValue] == 0) {
                static NSString *fourthReuse = @"fourthReuse";
                LTFReadFourthCell *cell = [tableView dequeueReusableCellWithIdentifier:fourthReuse];
                if (!cell) {
                    cell = [[[LTFReadFourthCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:fourthReuse] autorelease];
                }
                [cell.picImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
                cell.titleLabel.text = model.title;
                cell.sort.text = model.source;
                return cell;
            }
            else {
                return nil;
            }
        }
    }
    else {
        static NSString *secondReuse = @"secondReuse";
        LTFReadSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:secondReuse];
        if (!cell) {
            cell = [[[LTFReadSecondCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:secondReuse] autorelease];
        }
        [cell.firstImageView setImageWithURL:[NSURL URLWithString:[model.imgnewextra firstObject][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        [cell.secondImageView setImageWithURL:[NSURL URLWithString:[model.imgnewextra lastObject][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        [cell.thirdImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        cell.titleLabel.text = model.title;
        cell.sort.text = model.source;
        
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFReadModel *model = self.tableViewArray[indexPath.row];
    if (model.imgnewextra == nil) {
        if (model.imgsrc == nil) {
            return 100;
        } else {
            if ([model.recType intValue] == 0) {
                return 300;
            }
            else {
            return 150;
            }
        }
    }
    else {
        return 300;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFReadModel *model = self.tableViewArray[indexPath.row];
    [self.delegate sendReadDocid:model.docid];
    
}


#pragma mark 对地址进行解析
- (void)createData {
    //初始化数组
    self.tableViewArray = [NSMutableArray array];
    NSString *str = @"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&passport=&devId=4gas4FUITAgpRvhzbu84niiRmsdhj%2Bjod7dwa50AQPdHYDcbIQdm9biImCah5hUU&size=20&version=6.0&spever=false&net=wifi&lat=&lon=&ts=1460460668&sign=5ad716d9U6F3XJuOIXcdlDP7rnFGumFV%2BcYaocOthr948ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstor";
    //调用解析方法
    [LTFNetWorkingTool toolWithUrl:str block:^(id result) {
        
        NSDictionary *dic = result;
        NSArray *recommend = dic[@"推荐"];
        // NSLog(@"%@", [recommend class]);
        //调用model
        self.tableViewArray = [LTFReadModel BaseModelByArray:recommend];
        [self.tableView reloadData];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
